//
//  CityViewController.m
//  MeiLiProductModel
//
//  Created by B505 on 15/2/22.
//  Copyright (c) 2015年 孙晓林. All rights reserved.
//

#import "CityViewController.h"
#import "SliderViewController.h"
#import "LeftMenuViewController.h"
#import "T4Marco.h"
#import "UIButton+Utils.h"

#define SEARCHHEIGHT 40
#define HEADERLABEL_X 15
#define HEADERLABEL_Y 16
#define HEADERLABEL_WIDTH 100
#define HEADERLABEL_HEEIGHT 20

static  NSString * const ChangeCityNotificationName = @"city";
static  NSString * const CityKeyName = @"cityname";

@interface CityViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
{
    NSArray *headerTitles;
    NSArray *tempDataArray;
    NSMutableArray *dataArray;
    NSMutableArray *searchResults;
}

@property (weak, nonatomic) IBOutlet UINavigationBar *cityNavBar;
@property (weak, nonatomic) IBOutlet UITableView *cityTableView;

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchController *searchController;

@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    tempDataArray = @[@"北京",@"上海", @"广州", @"深圳", @"南京", @"杭州", @"苏州"];
    headerTitles = @[@"定位城市", @"热门城市"];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
    [menuBtn setImageWithName:@"city_menu_btn"];
    [menuBtn addTarget:self action:@selector(onClickMenuBtn:)];
    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc]
                                        initWithCustomView:menuBtn];
    _cityNavBar.topItem.leftBarButtonItem = leftBarBtnItem;
    _cityNavBar.topItem.title = @"切换城市";
    
    
    _cityTableView.delegate = self;
    _cityTableView.dataSource = self;
    
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SEARCHHEIGHT)];
    _searchBar.delegate=self;
    [_searchBar setPlaceholder:@"搜索城市"];
    _cityTableView.tableHeaderView = _searchBar;
    
}

- (void)onClickMenuBtn:(UIButton *)btn
{
    T4_LOG_FUNC();
    [[SliderViewController sharedSliderController] showLeftViewController];
}

#pragma mark ----------------UITableviewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numberOfRows;
    if (section == 0) {
        numberOfRows = 1;
    }else{
        numberOfRows = tempDataArray.count * 2;
    }
    return numberOfRows;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *cellBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    cellBackView.backgroundColor = [UIColor whiteColor];
    cellBackView.alpha = 0.95;
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(HEADERLABEL_X, HEADERLABEL_Y, HEADERLABEL_WIDTH, HEADERLABEL_HEEIGHT);
    label.textColor = [UIColor grayColor];
    label.text = headerTitles[section];
    label.font = [UIFont systemFontOfSize:15];
    [cellBackView addSubview:label];
    return cellBackView;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSArray *indexTitleArray = @[@"#",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R", @"S",@"K",@"U",@"V",@"W",@"X",@"Y",@"Z"];
    return indexTitleArray;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    if (indexPath.section == 0) {
        cell.layer.borderColor = HEXCOLOR(0xE3E3E3).CGColor;
        cell.layer.borderWidth = .8;
        cell.textLabel.text = tempDataArray[0];
    }else{
        if (indexPath.row == 0) {
            cell.layer.borderColor = HEXCOLOR(0xE3E3E3).CGColor;
            cell.layer.borderWidth = .8;
        }
        cell.textLabel.text = tempDataArray[indexPath.row % 7];
    }
    
    return cell;
}

#pragma mark ----------------UITableviewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     NSDictionary *info = @{CityKeyName:tempDataArray[indexPath.row %7]};
    [[NSNotificationCenter defaultCenter] postNotificationName:ChangeCityNotificationName object:self userInfo:info];
    [[SliderViewController sharedSliderController]showContentControllerWithModel:@"MenuViewController"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
